﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Common.DbConstants;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using BusinessEntity.Common;
using Common;

namespace DataAccess.Common
{
    public class CommonAdapter
    {
        //public static DataSet GetCodeDescription(string sessionId, CodeDescription code, ref DateTime? lastRefreshed, out string successMessage, out string errorMessage)
        //{
        //    successMessage = string.Empty;
        //    errorMessage = string.Empty;
        //    try
        //    {
                
        //        SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
        //        SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();

        //        SqlParameter outLastRefreshed = new SqlParameter(DbParameterReferenceData.REFRESHEDATETIME, SqlDbType.DateTime);
        //        outLastRefreshed.Direction = ParameterDirection.Output;
                
        //        DataSet ds = SqlHelper.ExecuteDataset(Utility.GetApplicationConnectionString(),
        //                                                CommandType.StoredProcedure,
        //                                                DbStoredProcedures.GETREFDATA,
        //                                                new SqlParameter(DbParameters.SESSIONID, sessionId),
        //                                                new SqlParameter(DbParameterReferenceData.GLOBALDATAKEYID, (int)code),
        //                                                new SqlParameter(DbParameterReferenceData.LASTREFRESHEDATETIME, lastRefreshed),
        //                                                outLastRefreshed,
        //                                                outputSuccessMessage,
        //                                                outputErrorMessage
        //                                                );

        //        if (outputSuccessMessage.Value != null)
        //        {
        //            successMessage = outputSuccessMessage.Value.ToString();
        //        }

        //        if (outputErrorMessage.Value != null)
        //        {
        //            errorMessage = outputErrorMessage.Value.ToString();
        //        }

        //        if (outputErrorMessage.Value != null)
        //        {
        //            lastRefreshed = Utility.GetNullableDate(outLastRefreshed.Value.ToString());
        //        }
                
        //        return ds;

        //    }
        //    catch (Exception ex)
        //    {
        //        errorMessage = Utility.GetExceptionMessageString(ex);
        //        return null;
                
        //    }
        //}
        //public static DataSet GetCodeDescription(string sessionId, CodeDescription code)
        //{
        //    try
        //    {
        //        string sptoCall = string.Empty;
        //        switch (code)        
        //        {
                                   
        //            case CodeDescription.Physicians:
        //                 sptoCall = DbStoredProcedureDropDown.GETPHYSICIANS;
        //                 break;
        //            case CodeDescription.OverallStatus:
        //                 sptoCall = DbStoredProcedureDropDown.GETOVERALLSTATUS;
        //                 break;
        //            case CodeDescription.Laboratory:
        //                 sptoCall = DbStoredProcedureDropDown.GETLABORATORY;
        //                 break;
        //            case CodeDescription.ResultName:
        //                 sptoCall = DbStoredProcedureDropDown.GETRESULTNAME;
        //                 break;
        //            case CodeDescription.ResultUnit:
        //                 sptoCall = DbStoredProcedureDropDown.GETRESULTUNIT;
        //                 break;
        //            case CodeDescription.LabTestFlag:
        //                 sptoCall = DbStoredProcedureDropDown.GETLABTESTFLAG;
        //                 break;                   
        //            default:
        //                break;
        //        }

        //        DataSet ds = SqlHelper.ExecuteDataset(Utility.GetApplicationConnectionString(),
        //                        CommandType.StoredProcedure, 
        //                       sptoCall                                
        //                       );

        //        return ds;

        //    }
        //    catch (SqlException sqlex)
        //    {
        //        throw sqlex;
        //    }
        //}

        internal static Log SetCreateUpdatedLog(IDataReader reader)
        {

            Log log = new Log();

            if (reader[DbColumnNames.CREATEDBY] != null && reader[DbColumnNames.CREATEDBY].ToString().Length > 0)
            {
                log.CreatedBy = reader[DbColumnNames.CREATEDBY].ToString();
            }

            if (reader[DbColumnNames.CREATEDON] != null && reader[DbColumnNames.CREATEDON].ToString().Length > 0)
            {
                log.CreatedOn = Convert.ToDateTime(reader[DbColumnNames.CREATEDON].ToString());
            }

            if (reader[DbColumnNames.UPDATEDBY] != null && reader[DbColumnNames.UPDATEDBY].ToString().Length > 0)
            {
                log.UpdatedBy = reader[DbColumnNames.UPDATEDBY].ToString();
            }

            if (reader[DbColumnNames.UPDATEDON] != null && reader[DbColumnNames.UPDATEDON].ToString().Length > 0)
            {
                log.UpdatedOn = Convert.ToDateTime(reader[DbColumnNames.UPDATEDON].ToString());
            }

            return log;
        }
       
    }
}
